package com.newasia.management.services;

import com.newasia.management.ISysDealerService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.dal.user.persistence.SysUserMapper;
import com.newasia.management.dto.dealer.InvitedCodeRequest;
import com.newasia.management.dto.dealer.InvitedCodeResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class SysDealerServiceImpl implements ISysDealerService {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public InvitedCodeResponse getInvitedCode(InvitedCodeRequest request) {
        InvitedCodeResponse response = new InvitedCodeResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());

        try{
            Long userId = request.getUserId();

            if(sysUserMapper.checkMenuByUserId(userId)==null){
                response.setCode(ManagementRetCode.SYSTEM_MENU_ERROR.getCode());
                response.setMsg(ManagementRetCode.SYSTEM_MENU_ERROR.getMessage());
                return response;
            }

            String key = null;
            if(!redissonClient.getBucket(userId.toString()).isExists()) {
                key = getStringRandom();
                RBucket rBucket = redissonClient.getBucket(userId.toString());
                rBucket.set(key);
                rBucket.expire(15, TimeUnit.MINUTES);
                rBucket = redissonClient.getBucket(key);
                rBucket.set(userId);
                rBucket.expire(15, TimeUnit.MINUTES);
            }else{
                RBucket rBucket = redissonClient.getBucket(userId.toString());
                rBucket.expire(15, TimeUnit.MINUTES);
                key = rBucket.get().toString();
                rBucket = redissonClient.getBucket(key);
                rBucket.expire(15, TimeUnit.MINUTES);
            }
            response.setInvitedCode(key);
        }catch (Exception e){
            log.error("SysUserServiceImpl.selectUserByName Occur Exception :"+e);
            e.printStackTrace();
            response.setCode(ManagementRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ManagementRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }

    public static String getStringRandom() {
        int length = 3;
        String val = "";
        Random random = new Random();

        //参数length，表示生成几位随机数
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("SSS");
        String time = sdf.format(new Date());
        return val+time;
    }
}
